import tkinter as tk
from tkinter import messagebox, ttk

from language_utils import LanguageSwitcher
from setting_page import SettingFrame
from testing_page import TestingFrame
from config_manager import ConfigManager  # 导入 ConfigManager

class main_page:
    def __init__(self, master):
        self.master = master

        # 创建样式并设置标签字体大小
        style = ttk.Style()
        # 创建语言切换器
        self.language_switcher = LanguageSwitcher(self.master, position="bottom-right")
        self.master.title(self.language_switcher.lang_manager.get_text("common", "title"))
        style.configure("TNotebook.Tab", font=('Arial', 12, 'bold'), padding=(10, 5))  # 设置字体大小
        # 初始化 ConfigManager
        self.config_manager = ConfigManager()
        # 创建界面
        self.create_widgets()
        # 注册语言切换回调函数
        self.language_switcher.lang_manager.register_callback(self.update_tab_texts)

    def update_tab_texts(self):
        """更新标签页的文本"""
        notebook = self.master.nametowidget("klay_notebook")  # 获取名为"klay_notebook"的Notebook对象
        notebook.tab(self.Test_Frame, text=self.language_switcher.lang_manager.get_text("common", "test_tab"))
        notebook.tab(self.Setting_Frame, text=self.language_switcher.lang_manager.get_text("common", "setting_tab"))

    def create_widgets(self):
        resolution = self.master.maxsize()
        width = int(resolution[0] * 0.8)
        height = int(resolution[1] * 0.8)
        self.master.geometry(f"{width}x{height}")
        self.master.config(bg="#FFFFFF")
        self.master.attributes("-topmost", True)  # 置顶窗口
        notebook = ttk.Notebook(self.master, name="klay_notebook")
        notebook.pack(fill="both", expand=True)

        # 创建标签页
        self.Test_Frame = ttk.Frame(notebook)
        TestingFrame(self.Test_Frame, self.language_switcher, self.config_manager)  # 传递 ConfigManager

        self.Setting_Frame = ttk.Frame(notebook)
        SettingFrame(self.Setting_Frame, self.language_switcher, self.config_manager)  # 传递 ConfigManager

        # 添加标签页到Notebook
        notebook.add(self.Test_Frame, text=self.language_switcher.lang_manager.get_text("common", "test_tab"))
        notebook.add(self.Setting_Frame, text=self.language_switcher.lang_manager.get_text("common", "setting_tab"))

        # 布局配置
        self.master.columnconfigure(0, weight=1)
        self.master.rowconfigure(1, weight=1)

if __name__ == "__main__":
    root = tk.Tk()
    app = main_page(root)
    root.mainloop()